﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Arm2015.Storage
{
    public static class Journal
    {
        public static IQueryable<Models.Journal.Row> GetJournal(int contractId, DateTime dateStart, DateTime dateEnd)
        {
            var context = new Storage.DataEntities();

            var result = context.D_JOURNAL
                .Where(x => x.D_OBJECT_ELEMENT.D_OBJECT.D_OBJECT_CONTRACT.FirstOrDefault().C_CONTRACT == contractId)
                .Where(x => x.DATE >= dateStart && x.DATE <= dateEnd)
                .Select(x => new Models.Journal.Row
                {
                    C_JOURNAL = x.C_JOURNAL,
                    DATE = x.DATE,
                    C_MAKER = x.C_MAKER,
                    N_MAKER = x.D_MAKER.DISPLAY_NAME,
                    C_OBJECT = x.D_OBJECT_ELEMENT.C_OBJECT,
                    N_OBJECT = x.D_OBJECT_ELEMENT.D_OBJECT.OBJECT_CAPTION,
                    C_ELEMENT = x.D_OBJECT_ELEMENT.C_ELEMENT,
                    C_TYPE_ELEMENT = x.D_OBJECT_ELEMENT.S_ELEMENT.C_TYPE_ELEMENT,
                    N_ELEMENT = x.D_OBJECT_ELEMENT.S_ELEMENT.N_ELEMENT,
                    C_OBJECT_ELEMENT = x.C_OBJECT_ELEMENT,
                    LOCATION = x.D_OBJECT_ELEMENT.LOCATION,
                    BEGKM = x.BEGKM,
                    ENDKM = x.ENDKM,
                    C_WORK = x.C_WORK,
                    N_WORK = x.S_WORK.N_WORK,
                    C_UNIT = (int)x.S_WORK.C_UNIT,
                    N_UNIT = x.S_WORK.S_UNIT.N_UNIT,
                    DECIMAL_PLACES = x.S_WORK.S_UNIT.COMMA,
                    WORK_VALUE = Math.Round(x.VALUE, x.S_WORK.S_UNIT.COMMA),
                    COSTONEWORK_NOW = (double)x.COSTONEWORK_NOW,
                    WORK_COST = Math.Round((Math.Round(x.VALUE, x.S_WORK.S_UNIT.COMMA) * Math.Round((double)x.COSTONEWORK_NOW, 2)), 2)
                });
            return result;
        }
    }
}
